package com.synnex.mobile.servier.web.system;

import com.synnex.mobile.generic.core.json.JSONModel;
import com.synnex.mobile.generic.web.anotation.mapping.RequestMapping;
import com.synnex.mobile.generic.web.util.WebUtils;
import com.synnex.mobile.generic.web.view.ModelAndView;
import com.synnex.mobile.servier.core.config.ServierConfigHelper;
import com.synnex.mobile.servier.web.common.controller.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 12-7-12
 * Time: 下午4:47
 * To change this template use File | Settings | File Templates.
 */
@RequestMapping(url = "/system")
public class SystemController extends AbstractController {


//        protected Cache cache;
//
//        public void setCache(Cache cache) {
//            this.cache = cache;
//        }

    @RequestMapping(url = "/refreshCache.do")
    public ModelAndView refreshCache(HttpServletRequest request, HttpServletResponse response) {
        String requestIP = WebUtils.getIpAddr(request);
        logger.info("Refresh systemconfig and cache from IP:" + requestIP);
        String status = null;
        if (WebUtils.isInternalIP(requestIP)) {
//                cache.removeAll();
            this.configService.clearHelperCache();
            ServierConfigHelper h = this.getConfig();
            status = "success";
        } else {
            status = "error, not internal IP.";
        }
        return createJSONModelAndView(JSONModel.create().setData(status));
    }

//    @RequestMapping(url = "/clearCaches.do")
//    public ModelAndView clearCaches(HttpServletRequest request, HttpServletResponse response) {
//        String requestIP = WebUtils.getIpAddr(request);
//        String status = null;
//        if (WebUtils.isInternalIP(requestIP)) {
//            List<String> urls = this.configService.getHelper(SystemKeys.APP_NAME, ServierConfigHelper.class).getClusterUrls();
//            for (String url : urls) {
//                URLUtils.getData(url);
//            }
//            status = "success";
//        } else {
//            status = "error, not internal IP.";
//        }
//        return createJSONModelAndView(JSONModel.create().setData(status));
//    }

    @RequestMapping(url = "/config.do")
    public ModelAndView getConfig(HttpServletRequest request, HttpServletResponse response) {

        Map<String,Object> model = new HashMap<String,Object>();
        String link = "http://"+this.getConfig().getFtpHost()+this.getConfig().getFtpDownLoadPath()+"/";
        model.put("link",link);

        return createJSONModelAndView(JSONModel.create().setData(model));
    }


}
